<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * User Controller
 * @author Simon, Dennis, Denis
 *
 */
class User extends CI_Controller {

	function User()
	{

		parent::__construct();
// 		$this->load->model('user_model');
		session_start();
		if(!isset($_SESSION['username'])){
			$_SESSION['username']='';
			$_SESSION['password']='';
			$_SESSION['id']='';
		}
		log_message('debug', "User Class Initialized");
	}

	/**
	 * method to return the user id from current session
	 * @return int id
	 */
	function getID()
	{

		return $_SESSION['id'];

	}

	/**
	 * Login User
	 *
	 * @access	public
	 * @param	string	the username
	 * @param	string	the password
	 * @return	boolean	if the user was logged in or not
	 */
	function login( $username, $password )
	{
		if($this->user_model->checkUserAndPassword(mysql_real_escape_string( $username ),sha1( md5( $password ) ))){
			$_SESSION['id']=$this->user_model->getUserByUsername($username)->id;
			$_SESSION['username']=$username;
			$_SESSION['password']=sha1( md5( $password ) );
			return TRUE;
		} else {
			return false;
		}

	}

	/**
	 * Logged in
	 *
	 * @access	public
	 * @return	boolean	if the user is logged in or not
	 */
	function logged_in( )
	{
		$username = $_SESSION['username'];
		$password = $_SESSION['password'];
		// 		echo "Username: $username";
		// 		echo "Password: $password";
		return $this->user_model->checkUserAndPassword($username , $password);

	}

	/**
	 * method to register a new user. stores the new user in the DB
	 * @param string $username
	 * @param string $password
	 * @param string $mail
	 * @param int $status
	 * @param object $userdata
	 */
	function register($username,$password,$mail,$status,$userdata)
	{
		$ip='';
		$surname=$userdata->Surname;
		$name=$userdata->Name;
		$pro=$userdata->Pro;
		$size=$userdata->Size;
		$weight=$userdata->Weight;
		$gender=$userdata->Gender;
		$city=$userdata->City;
		$country=$userdata->Country;
		$birthday=$userdata->Birthday;
		$this->user_model->checkUserAndPassword($username , $password);
// 				$this->load->model('user_model');
		return $this->user_model->addUser($username, $password,$mail,$ip,$status,$surname,$name,$pro,$size,$weight,$gender,$city,$country,$birthday);

	}
	
	/**
	 * method to check if a username already exists in the database
	 * returns true or false
	 * @param string $username
	 */
	function checkIfUsernameExist($username){
		
		return $this->user_model->checkIfUsernameExist($username);
		
	}

	/**
	 * method to logout. clears the current session
	 */
	function logout(){
		$_SESSION['username'] = '';
		$_SESSION['password'] = '';
		$_SESSION['id'] = '';
		session_destroy();
	}

	/**
	 * method to delete one user by the current user id
	 */
	function remove(){
		$this->user_model->deleteUser($_SESSION['id']);
	}

}